
#include <Inklude/ikCHttpClient.h>
#include <boost/network/protocol/http/client.hpp>
#include <string>
#include <sstream>
#include <iostream>

using namespace Inklude;
using namespace std;

namespace http = boost::network::http;

http::client* _cvtClient(void* pClient)
{
    return reinterpret_cast<http::client*>(pClient);
}

http::client::response* _cvtResponse(void *pResponse)
{
    return reinterpret_cast<http::client::response*>(pResponse);
}

ikCHttpClient::ikCHttpClient()
{
    m_pClient = NULL;
    m_pResponse = NULL;
    m_pClient = (void*)(new http::client);
    m_pResponse = (void*)(new http::client::response);
}

ikCHttpClient::~ikCHttpClient()
{
    auto pClient = _cvtClient(m_pClient);
    auto pResponse = _cvtResponse(m_pResponse);
    SAFE_DELETE(pClient);
    SAFE_DELETE(pResponse);
}

void ikCHttpClient::SetHost(const char* HostName)
{
    m_strHostName.clear();
    m_strHostName.assign(HostName);
}

void ikCHttpClient::SetScriptFile(const char* ScriptFilePath)
{
    m_strScriptFile = ScriptFilePath;
}

std::string ikCHttpClient::GetHost() const
{
    return m_strHostName;
}

std::string ikCHttpClient::GetScriptFile() const
{
    return m_strScriptFile;
}

bool ikCHttpClient::SendRequest()
{
    bool retVal = true;
    try
    {
        std::ostringstream _url;
        if (m_userName.size() > 0 && m_password.size() > 0)
        {
            _url << "http://";
            _url << m_userName.c_str();
            _url << ":";
            _url << m_password.c_str();
            _url << "@";
        }

        _url << m_strHostName.c_str();
        _url << m_strScriptFile.c_str() << "?";
	
	    int sz = m_vctVars.size();
	    for(int i=0;i<sz;i++) {
            _url << m_vctVars[i].c_str();
            _url << "=";
            _url << m_vctVals[i].c_str();
            if(i != (sz-1)) 
            {
                _url << "&";
		    }    
        }
               
        http::client::request _req(_url.str());
        auto pClient = _cvtClient(m_pClient);
        auto pResponse = _cvtResponse(m_pResponse);
        (*pResponse) = pClient->get(_req);
    }
    catch (std::exception e)
    {
        cout << e.what() << endl;
        retVal = false;
    }
    return retVal;
}

int ikCHttpClient::GetResponse(void* pBuffer, unsigned int nResponseSize)
{
    auto pResponse = _cvtResponse(m_pResponse);
    if (pResponse->body().length() < nResponseSize)
    {
        memcpy_s(pBuffer, nResponseSize, pResponse->body().data(), pResponse->body().length());
        return (int)(pResponse->body().length());
    }
    else
    {
        return -1;
    }
}

void ikCHttpClient::PutVariable(const char* VariableName,const char* Value) 
{
    m_vctVars.push_back(VariableName);
    m_vctVals.push_back(Value);
	
    /*if(isDuplicateVar(VariableName)) {
		string str = "Duplicate variable name - ";
		str = str + VariableName;
		throw ikCWebFormWinAPIException(str.c_str());
	}else {
		m_vctVars.push_back(VariableName);
		m_vctVals.push_back(Value);
	}*/
}

unsigned long ikCHttpClient::GetBytesRead()
{
    return m_lBytesRead; 
}

void ikCHttpClient::Reset()
{
    m_vctVars.clear();
    m_vctVals.clear();
}

void ikCHttpClient::SetUserName(const char* UserName)
{
    m_userName.clear();
    m_userName.assign(UserName);
}

void ikCHttpClient::SetPassword(const char* Password)
{
    m_password.clear();
    m_password.assign(Password);
}


